using System;
using System.Collections.Generic;
using System.Text;
using Adoor.Metadata;
using System.Xml;
using System.IO;

namespace Adoor.Wizard
{
    public class DictionaryAppenderWriterProvider : IXmlWriterProvider
    {
        public class XmlStringBuilderWriter : XmlWriterDecorator
        {
            StringBuilder sb;


            public XmlStringBuilderWriter(StringBuilder sb)
                : base(null)
            {
                this.sb = sb;
                XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(this.sb));
                xmlTextWriter.Formatting = Formatting.Indented;
                xmlTextWriter.Indentation = 8;
                this.Principal = xmlTextWriter;
            }

            public override void Flush()
            {
                base.Flush();
            }

            public override void Close()
            {
                Flush();
                base.Close();
            }
        }

        public DictionaryAppenderWriterProvider()
        {
            dic = new Dictionary<string, StringBuilder>();
        }

        private Dictionary<string, StringBuilder> dic;

        public Dictionary<string, StringBuilder> Datas
        {
            get { return dic; }
        }

        #region IXmlWriterProvider Members

        public System.Xml.XmlWriter GetWriter(string name)
        {
            while (dic.ContainsKey(name))
                name += '_';

            StringBuilder sb = new StringBuilder();
            XmlWriterDecorator result = new XmlStringBuilderWriter(sb);
            dic[name] = sb;
            return result;
        }

        #endregion
    }
}
